﻿@page "/auth"

@using Microsoft.AspNetCore.Authorization
@using BlazorApp1.Data
@using Microsoft.EntityFrameworkCore
@inject ApplicationDbContext Context;
@inject UserService UserService;
@rendermode InteractiveServer

@attribute [Authorize]

<PageTitle>用户管理</PageTitle>

<AuthorizeView>
    <div class="row">
        <div class="col-md-2">
            <label>Id: </label>
            <div class="input-group">
                <input class="form-control" readonly="readonly" @bind="_newUser.Id"/>
                <button class="btn btn-primary" @onclick="ReadCard">读卡</button>
            </div>
        </div>
        <div class="col-md-2">
            <label>姓名: </label>
            <input class="form-control" @bind="_newUser.Name"/>
        </div>
        <div class="col-md-2">
            <label>学号: </label>
            <input class="form-control" @bind="_newUser.StudentId"/>
        </div>
        <div class="col-md-2">
            <label>班级: </label>
            <input class="form-control" @bind="_newUser.Class"/>
        </div>
        <div class="col-md-2">
            <label>联系方式: </label>
            <input class="form-control" @bind="_newUser.Phone"/>
        </div>
        <div class="col-md-2">
            <label>备注: </label>
            <input class="form-control" @bind="_newUser.Remark"/>
        </div>
    </div>
    <div class="row"></div>
    <div class="row">
        <div class="col-md-4">
            <button class="btn btn-primary" disabled="@(!AllFieldsFilled())" @onclick="AddDevice">添加用户</button>
        </div>
    </div>
    <table class="table">
        <thead>
        <tr>
            <th>Id</th>
            <th>名称</th>
            <th>学号</th>
            <th>班级</th>
            <th>联系方式</th>
            <th>备注</th>
            <th>操作</th>
        </tr>
        </thead>
        <tbody>
        @foreach (var forecast in forecasts)
        {
            <tr>
                <td>@forecast.Id</td>
                <td>@forecast.Name</td>
                <td>@forecast.StudentId</td>
                <td>@forecast.Class</td>
                <td>@forecast.Phone</td>
                <td>@forecast.Remark</td>
                <td><button class="btn btn-primary" @onclick="() => DeleteDevice(forecast)">删除</button></td>
            </tr>
        }
        </tbody>
    </table>
</AuthorizeView>

@code {
    private List<UserInfo> forecasts;

    protected override async Task OnInitializedAsync()
    {
        forecasts = await Context.UserInfo.ToListAsync();
    }

    private void Action(string id)
    {
        if (id != _newUser.Id)
        {
            InvokeAsync(() =>
            {
                _newUser.Id = id;
                StateHasChanged();
            });
        }
    }

    UserInfo _newUser = new();
    
    private async void AddDevice()
    {
        await Context.AddAsync(_newUser);
        await Context.SaveChangesAsync();
        forecasts.Add(_newUser);
        _newUser = new UserInfo(); // Clear the input fields after adding
    }
    
    private async void DeleteDevice(UserInfo device)
    {
        Context.Remove(device);
        await Context.SaveChangesAsync();
        forecasts.Remove(device);
    }
    
    bool AllFieldsFilled()
    {
        return !string.IsNullOrWhiteSpace(_newUser.Id) &
               !string.IsNullOrWhiteSpace(_newUser.StudentId) &
               !string.IsNullOrWhiteSpace(_newUser.Class) &
               !string.IsNullOrWhiteSpace(_newUser.Phone) &
               !string.IsNullOrWhiteSpace(_newUser.Name);
    }

    private void ReadCard()
    {
        _newUser.Id = UserService.ReadCard();
    }
}